import { _decorator, Component, instantiate, Node, UITransform, v3 } from 'cc';
const { ccclass, property } = _decorator;

import { TurkishTetris_GameData } from "../../../../hall/Data/TurkishTetris_GameData";

@ccclass('TurkishTetrisFloorTipCom')
export class TurkishTetris_FloorTipCom extends Component {
    private tipItemNode: Node;
    private layoutNode: Node;
    private startX: number = 10;
    private itemWidth: number;
    private gapX: number;
    protected onLoad(): void {
        // super.onLoad();
        this.tipItemNode = this.findNode("item");
        this.layoutNode = this.findNode("Node_layout");
        this.itemWidth = this.tipItemNode.getComponent(UITransform).width;
        this.gapX = 10;
    }
    findNode(nodeName: string, parent?: Node): Node {
        let fromNode = parent;
        if (fromNode == null) {
            fromNode = this.node;
        }
        let node = this.findDeep(fromNode, nodeName);
        if (node == null) {
            return null;
        }
        return node;
    }

    findDeep(node: Node, nodeName: string): Node {
        if (node.name == nodeName) return node;
        for (let i = 0; i < node.children.length; i++) {
            let res = this.findDeep(node.children[i], nodeName);
            if (res) return res;
        }
        return null;
    }

    //    /**
    //     * 展示最下面即将出来的一行的方块的映射图形
    //     */
    showFloorTip() {
        let cardGroups = TurkishTetris_GameData.arrCardGroups_next;

        this.layoutNode.removeAllChildren();

        for (const cardGroup of cardGroups) {
            let x = cardGroup.x;
            let len = cardGroup.length;

            let tmpItem = instantiate(this.tipItemNode);
            tmpItem.getComponent(UITransform).width = len * this.itemWidth + (len - 1) * this.gapX;

            // tmpItem.x = this.startX + (this.itemWidth + this.gapX) * x;
            // tmpItem.y = 0;
            tmpItem.position = v3(this.startX + (this.itemWidth + this.gapX) * x, 0);

            tmpItem.active = true;
            tmpItem.parent = this.layoutNode;
        }
    }
}